//
//  ZSY_CityHunterCItyViewController.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 16/1/8.
//  Copyright © 2016年 张诗宇. All rights reserved.
//

#import "ZSY_CityHunterCItyViewController.h"
#import "ZSY_CityHunterCItyView.h"
#import "ZSY_CHAllCityTableViewCell.h"
#import "ZSY_CHHotCityTableViewCell.h"

@interface ZSY_CityHunterCItyViewController ()<UITableViewDataSource, UITableViewDelegate, ZSY_CHHotCityTableViewCellDelegate>

@property (nonatomic, retain) ZSY_BaseTableView *myTableView;
@property (nonatomic, retain) NSArray *array;

@end

@implementation ZSY_CityHunterCItyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    
    self.view.backgroundColor = [UIColor colorWithRed:63 / 255.0 green:176 / 255.0 blue:192 / 255.0 alpha:1];
    
    self.myTableView = [[ZSY_BaseTableView alloc] initWithFrame:CGRectMake(0, 100, WIDTH, 400) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor colorWithRed:63 / 255.0 green:176 / 255.0 blue:192 / 255.0 alpha:1];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    [self.myTableView registerClass:[ZSY_CHHotCityTableViewCell class] forCellReuseIdentifier:@"hotcityCell"];
    [self.myTableView registerClass:[ZSY_CHAllCityTableViewCell class] forCellReuseIdentifier:@"allcityCell"];
    self.array = [NSArray array];
    self.array = @[@"澳大利亚", @"巴黎", @"包头", @"北京", @"长沙", @"成都", @"重庆", @"川崎", @"大理", @"大连", @"大兴安岭市", @"东京", @"佛山", @"广州", @"贵阳", @"哈尔滨", @"海口", @"函馆", @"杭州", @"赫尔辛基", @"合肥", @"横滨", @"济南", @"京都", @"昆明", @"兰州", @"拉萨", @"丽江", @"伦敦", @"美国", @"明尼阿波利斯", @"牡丹江市", @"南京", @"青岛", @"清迈", @"日本", @"三亚", @"厦门", @"上海", @"山梨县", @"沈阳", @"深圳", @"台北", @"泰国", @"天津", @"土耳其", @"温哥华", @"武汉", @"乌鲁木齐", @"无锡", @"西安", @"香港", @"悉尼", @"西双版纳", @"许昌", @"银川", @"张家界", @"珠海", @"淄博"];
    
    // 标题
    ZSY_BaseLabel *titleLabel = [[ZSY_BaseLabel alloc] initWithFrame:CGRectMake((WIDTH - 100)/2, 30, 100, 36)];
    titleLabel.text = @"选择城市";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:titleLabel];
    
    // 返回按钮
    ZSY_BaseButton *backButton = [ZSY_BaseButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake((WIDTH - 20) / 2, HEIGHT - 80, 20, 20);
    [backButton setImage:[UIImage imageNamed:@"iconfont-cha"] forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
    }
    else{
        
        return self.array.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        
        ZSY_CHHotCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hotcityCell"];
        cell.selectionStyle = NO;
        cell.myDelegate = self;
        return cell;
    }else{
        
        ZSY_CHAllCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"allcityCell"];
        cell.selectionStyle = NO;
        cell.cityName = [self.array objectAtIndex:indexPath.row];
        return cell;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return 230;
    }
    else{
        
        return 36;
    }
}

// 协议方法
- (void)getCityName:(NSString *)string{
    
    //    NSLog(@"%@", string);
    
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"city"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    //    NSString *city = [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];
    //    NSLog(@"city = %@", city);
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

// cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    NSLog(@"%@", string);
    
    NSString *string = [self.array objectAtIndex:indexPath.row];
    
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"city"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    //    NSString *city = [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];
    //    NSLog(@"city = %@", city);
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


// 取消模态
- (void)backButtonAction:(ZSY_BaseButton *)btn{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
